//This is the GameReview GUI object that represents a viewable panel representing game play stats

//import only what we need to speed things up
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;

public class GameReview extends JPanel implements ActionListener {
	
	//declare class instance vars
	private Image background;
	private Image scaledbg;
	private Dimension prevsize;
	private JButton next_btn;
	private JLabel winheader_lbl;
	private JLabel player1name_lbl;
	private JLabel player1stats_lbl;
	private JLabel player2name_lbl;
	private JLabel player2stats_lbl;
	private String player1name;
	private String player2name;
	//common colors
	public static Color bgnorm = new Color(188, 143, 70);
	public static Color fgnorm = new Color(56, 5, 3);
	public static Color bgalt1 = new Color(221, 173, 98);
	public static Color fgalt1 = new Color(93, 15, 2);
	public static Color fgalt2 = new Color(242, 79, 1);
	
	//our constructor
	public GameReview() {
		
		//call our ancestor
		super();
		
		//build our layout with GridBag
		//assuming an invisible grid of 14 x 10
		this.setLayout(new GridBagLayout());
		
		//add a filler panel to fill the entire first column (1st-10th row) (1st column)
		JPanel temppan1 = new JPanel();
		temppan1.setOpaque(false);
		temppan1.setVisible(true);
		this.add(temppan1, new GridBagConstraints( 0, 0, 1, 10, 0.1, 1.0, 
			GridBagConstraints.LINE_START, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
			0, 0));
		//add a filler panel to fill the entire first row, sorta (2nd-13th column) (1st row)
		JPanel temppan2 = new JPanel();
		temppan2.setOpaque(false);
		temppan2.setVisible(true);
		this.add(temppan2, new GridBagConstraints( 1, 0, 12, 1, 0.85, 0.05, 
			GridBagConstraints.PAGE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),
			0, 0));
		//add a filler panel to fill the entire last column (1st-10th row) (14th column)
		JPanel temppan3 = new JPanel();
		temppan3.setOpaque(false);
		temppan3.setVisible(true);
		this.add(temppan3, new GridBagConstraints(13, 0, 1, 9, 0.1, 1.0, 
			GridBagConstraints.LINE_END, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
			0, 0));
		//add our main panel to fill up center (2nd-13th column) (2nd-9th row)
		JPanel mainpan = new JPanel();
			//setup another GridBag
			//assuming an invisible grid of 12 x 10
			mainpan.setLayout(new GridBagLayout());
			//add winner header label to all of the top 2 row (1st-12th column) (1st-2nd row)
			this.winheader_lbl = new JLabel();
			this.winheader_lbl.setText("...Wins the Game!");
			this.winheader_lbl.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 24));
			this.winheader_lbl.setForeground(GameReview.fgnorm);
			mainpan.add(this.winheader_lbl, new GridBagConstraints( 0, 0, 12, 2, 1.0, 0.5, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,10,0,10),
				0, 0));
			//add general header label to all of the 3rd row (1st-12th column) (3rd row)
			JLabel genheader1 = new JLabel();
			genheader1.setText("Game Play Statistics for This Game");
			genheader1.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 20));
			genheader1.setForeground(GameReview.fgalt1);
			mainpan.add(genheader1, new GridBagConstraints( 0, 2, 12, 1, 1.0, 0.2, 
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0,10,0,10),
				0, 0));
			//build stat names label
			String stnames = "<html>" 
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"3\">Total Moves</font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\">Warps</font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\">Jumps</font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\">Kings Created</font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\">Kings Destroyed</font><br>"
				+ "</html>";
			//build stat spacers label
			String stspace = "<html>" 
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"3\"> . </font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\"> . </font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\"> . </font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\"> . </font><br>"
				+ "<font face=\"Courier New\" color=\"#380503\" size=\"2\"> . </font><br>"
				+ "</html>";
			//add player 1 name label to all of the 4th row (1st-12th column) (4th row)
			this.player1name_lbl = new JLabel();
			this.player1name_lbl.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 18));
			this.player1name_lbl.setForeground(GameReview.fgnorm);
			mainpan.add(this.player1name_lbl, new GridBagConstraints( 0, 3, 12, 1, 1.0, 0.2, 
			GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,10,0,10),
				0, 0));
			//setup player 1 stats centered in the remaining rows (1st-12th column) (5th-7th row)
				JLabel tmpstn1 = new JLabel(stnames);
				JLabel tmpsts1 = new JLabel(stspace);
				JLabel tmpsts2 = new JLabel(stspace);
				JLabel tmpsts3 = new JLabel(stspace);
				JLabel tmpsts4 = new JLabel(stspace);
				JLabel tmpsts5 = new JLabel(stspace);
				JLabel tmpsts6 = new JLabel(stspace);
				//add stat names in the left 3 columns (1st-3rd column) (5th-7th row)
				mainpan.add(tmpstn1, new GridBagConstraints( 0, 4, 3, 3, 0.6, 0.8, 
					GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0,10,0,0),
					0, 0));
				//add individual stat spacers in the center 6 columns (4th,5th,6th,7th,8th,9th column) (5th-7th row)
				mainpan.add(tmpsts1, new GridBagConstraints( 3, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts2, new GridBagConstraints( 4, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts3, new GridBagConstraints( 5, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts4, new GridBagConstraints( 6, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts5, new GridBagConstraints( 7, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts6, new GridBagConstraints( 8, 4, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				//add player 1 stats label in the right 3 columns (10th-12th column) (5th-7th row)
				this.player1stats_lbl = new JLabel();
				mainpan.add(this.player1stats_lbl, new GridBagConstraints( 9, 4, 3, 3, 0.6, 0.8, 
					GridBagConstraints.LINE_END, GridBagConstraints.BOTH, new Insets(0,0,0,10),
					0, 0));
			//add filler for all of 6th row (1st-12th column) (8th row)
			JLabel filler1 = new JLabel();
			filler1.setText("                 ");
			mainpan.add(filler1, new GridBagConstraints( 0, 7, 12, 1, 1.0, 0.1, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),
				0, 0));
			//add player 2 name label to all of the 7th row (1st-12th column) (9th row)
			this.player2name_lbl = new JLabel();
			this.player2name_lbl.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 18));
			this.player2name_lbl.setForeground(GameReview.fgnorm);
			mainpan.add(this.player2name_lbl, new GridBagConstraints( 0, 8, 12, 1, 1.0, 0.2, 
			GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,10,0,10),
				0, 0));
			//setup player 2 stats centered in the remaining rows (1st-12th column) (10th-12th row)
				JLabel tmpstn2 = new JLabel(stnames);
				JLabel tmpsts7 = new JLabel(stspace);
				JLabel tmpsts8 = new JLabel(stspace);
				JLabel tmpsts9 = new JLabel(stspace);
				JLabel tmpsts10 = new JLabel(stspace);
				JLabel tmpsts11 = new JLabel(stspace);
				JLabel tmpsts12 = new JLabel(stspace);
				//add stat names in the left 3 columns (1st-3rd column) (10th-12th row)
				mainpan.add(tmpstn2, new GridBagConstraints( 0, 9, 3, 3, 0.6, 0.8, 
					GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0,10,0,0),
					0, 0));
				//add individual stat spacers in the center 6 columns (4th,5th,6th,7th,8th,9th column) (10th-12th row)
				mainpan.add(tmpsts7, new GridBagConstraints( 3, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts8, new GridBagConstraints( 4, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts9, new GridBagConstraints( 5, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts10, new GridBagConstraints( 6, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts11, new GridBagConstraints( 7, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				mainpan.add(tmpsts12, new GridBagConstraints( 8, 9, 1, 3, 0.3, 0.8, 
					GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0),
					0, 0));
				//add player 1 stats label in the right 3 columns (10th-12th column) (10th-12th row)
				this.player2stats_lbl = new JLabel();
				mainpan.add(this.player2stats_lbl, new GridBagConstraints( 9, 9, 3, 3, 0.6, 0.8, 
					GridBagConstraints.LINE_END, GridBagConstraints.BOTH, new Insets(0,0,0,10),
					0, 0));
		mainpan.setBackground(GameReview.bgalt1);
		mainpan.setOpaque(true);
		mainpan.setVisible(true);
		this.add(mainpan, new GridBagConstraints( 1, 1, 12, 8, 1.0, 0.8, 
			GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),
			0, 0));
		//add a filler panel to fill the entire last row, sorta (2nd-11th column) (10th row)
		JPanel temppan4 = new JPanel();
		temppan4.setOpaque(false);
		temppan4.setVisible(true);
		this.add(temppan4, new GridBagConstraints( 1, 9, 10, 1, 0.85, 0.05, 
			GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),
			0, 0));
		//add our next button
		//we want this to be at the bottom right corner cell (10th row) (12th-13th column)
		this.next_btn = new JButton();
		this.next_btn.setText("Next");
		this.next_btn.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 20));
		this.next_btn.setBackground(GameReview.bgnorm);
		this.next_btn.setForeground(GameReview.fgnorm);
		this.next_btn.addActionListener(this);
		this.add(this.next_btn, new GridBagConstraints(11, 9, 2, 1, 0.0, 0.0, 
			GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0),
			0, 0));
			
		
		//set a reasonable size to match the ratio of our splash image
		this.setPreferredSize(new Dimension(500, 375));
		
		//try to get our splash image
		//if we cannot retrieve the image, it is somewhat serious
		//but I don't know what we could do from that point on :(
		try{
			background = ImageIO.read(new File("images\\SplashMedium.jpg"));
		}catch (IOException EXC){
			//this likely means there was some problem accessing the image file
		}catch (IllegalArgumentException EXC){
			//this likely means that for some reason we couldn't create the File object
		}
		
		//setup our text etc defaults
		//tbc
		ArrayList<String> tmpvsplayers = new ArrayList<String>();
			tmpvsplayers.add("Default Player1");
			tmpvsplayers.add("Default Player2");
			//this.setPlayers(tmpvsplayers);
			this.setWinner("Default Player1");
		this.setPlayersStats(5,4,3,2,1,5,4,3,2,1);
		
		//setup visibilities
		this.setVisible(true);
		this.setOpaque(true);
		
	}
	
	//override this so that we can customize drawing of the component
	//specifically so that we always have our pretty splash screen behind us :D
	public void paintComponent(Graphics g) {
		
		//call our ancestor
		super.paintComponent(g);
		
		System.out.println("$1GameLogon: painting...");
		
		//getSize is a retarded method, as such it requires
		//  we pass a Dimension object that it will use by referrence to store the size
		//the retarded part is that it also returns the same Dimension object
		//  go figure...
		Dimension panelsize = new Dimension(0,0);
		this.getSize(panelsize);
		
		//check if previous size has been declared yet
		if (this.prevsize == null){
			this.prevsize = new Dimension(panelsize);
		}
		
		//check if we need to rescale the image
		// this can occur if the component size changed
		// or it can occur if the image has not been scaled yet
		if (this.scaledbg == null || !panelsize.equals(this.prevsize)){
			this.scaledbg = background.getScaledInstance(panelsize.width,
			panelsize.height, Image.SCALE_SMOOTH);
		}
		g.drawImage(scaledbg, 0, 0, null);
		
		//reset the previous scale size
		this.prevsize = new Dimension(panelsize);
		
	}
	
	
	//hidden convenience method for adding the player names to the proper labels
	public void setPlayers(ArrayList<String> newplayers) {
		
		//clear out existing stats
		this.setPlayersStats(0,0,0,0,0,0,0,0,0,0);
		//dont do anything if the list is empty or not big enough
		if (newplayers.isEmpty() || newplayers.size()<2){
			return;
		}
		//extract the names
		this.player1name = newplayers.get(0);
		this.player2name = newplayers.get(1);
		this.player1name_lbl.setText(this.player1name);
		this.player2name_lbl.setText(this.player2name);
		this.getPlayersStats();
		
	}
	
	//hidden convenience method for adding the winning player name to the proper label
	public void setWinner(String player) {
		
		//check if there was a draw
		if (player == null){
			this.winheader_lbl.setText("The game is a draw!");
		}else{
			this.winheader_lbl.setText(player + " wins the game!!!");
		}
		
	}
	
	
	//hidden convenience method for setting the statistics of both players
	/*
	  int movestotal          int movestotal    
	  int warps               int warps         
	  int jumps               int jumps         
	  int kingscreated        int kingscreated  
	  int kingsdestroyed      int kingsdestroyed              
	*/
	private void setPlayersStats(ArrayList<Integer> stats) {
		
		//make sure we have enough data to build from
		if (stats.size() < 10){
			return;
		}
		//default, 5d0f02
		String txtdetail = "<html>" 
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"3\">" + stats.get(0) + "</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(1),stats.get(0)) + " (" + stats.get(1) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(2),stats.get(0)) + " (" + stats.get(2) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(3),stats.get(0)) + " (" + stats.get(3) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(4),stats.get(0)) + " (" + stats.get(4) + ")</font><br>"
			+ "</html>";
		this.player1stats_lbl.setText(txtdetail);
		txtdetail = "<html>" 
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"3\">" + stats.get(5) + "</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(6),stats.get(5)) + " (" + stats.get(6) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(7),stats.get(5)) + " (" + stats.get(7) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(8),stats.get(5)) + " (" + stats.get(8) + ")</font><br>"
			+ "<font face=\"Courier New\" color=\"#5d0f02\" size=\"2\">" + this.getPercentage(stats.get(9),stats.get(5)) + " (" + stats.get(9) + ")</font><br>"
			+ "</html>";
		this.player2stats_lbl.setText(txtdetail);
		
	}
	private void setPlayersStats(int movestotal1, int warps1, int jumps1, int kingscreated1, int kingsdestroyed1,
								int movestotal2, int warps2, int jumps2, int kingscreated2, int kingsdestroyed2) {
		
		ArrayList<Integer> tmpstats = new ArrayList<Integer>();
		tmpstats.add(new Integer(movestotal1));
		tmpstats.add(new Integer(warps1));
		tmpstats.add(new Integer(jumps1));
		tmpstats.add(new Integer(kingscreated1));
		tmpstats.add(new Integer(kingsdestroyed1));
		tmpstats.add(new Integer(movestotal2));
		tmpstats.add(new Integer(warps2));
		tmpstats.add(new Integer(jumps2));
		tmpstats.add(new Integer(kingscreated2));
		tmpstats.add(new Integer(kingsdestroyed2));
		this.setPlayersStats(tmpstats);
		
	}
	//hidden convenience method for getting the statistics of the current players
	private void getPlayersStats() {
		
		ArrayList<ArrayList<Integer>> tmpstat = UIController.getStats();
		//dont do anything if there are no stats
		if (tmpstat == null || tmpstat.isEmpty()){
			return;
		}
		ArrayList<Integer> joinedstat = new ArrayList<Integer>();
		for (int i=0; i<tmpstat.size(); ++i){
			for (int j=0; j<tmpstat.get(i).size(); ++j){
				joinedstat.add(tmpstat.get(i).get(j));
			}
		}
		this.setPlayersStats(joinedstat);
		
	}
	
	//hidden convenience method for getting the text percentage of 2 Integer objects
	private String getPercentage(Integer numerator, Integer denominator) {
		
		String percentage = "0%";
		try{
			percentage = "" + (int)Math.ceil(100 * (double)numerator.intValue() / (double)denominator.intValue()) + "%";
		}catch (ArithmeticException EXC){}
		return percentage;
		
	}
	
	//hidden convenience method for showing the user an information notice
	private void showInfoNotice(String info, String title) {
		
		this.repaint();
		JOptionPane.showMessageDialog(this, info, title, JOptionPane.INFORMATION_MESSAGE);
		
	}
	
	//hidden convenience method for asking the user a yes/no question
	private boolean askYNQuestion(String question, String title) {
		
		int response = JOptionPane.showConfirmDialog(this, 
			question, title, JOptionPane.YES_NO_OPTION,
			JOptionPane.QUESTION_MESSAGE);
		
		if (response == JOptionPane.YES_OPTION){
			return true;
		}else{
			return false;
		}
	}
	
	//hidden convenience method for cycling to the GameComplete window
	private void goNext() {
		
		ArrayList<String> tmpplayers = new ArrayList<String>();
		tmpplayers.add(this.player1name);
		tmpplayers.add(this.player2name);
		try{
			((GameComplete)UIController.grabState("GameComplete")).setPlayers(tmpplayers);
		}catch (NullPointerException EXC){
			System.out.println("Game Review failed to gain instance of GameComplete");
		}
		UIController.changeState("GameComplete");
		
	}
	
	//implement ActionListener
	public void actionPerformed(ActionEvent EVE) {
		
		//determine the caller
		Object tmpobj = EVE.getSource();
		if (tmpobj.equals(this.next_btn)){
			this.goNext();
		}
		
	}
	
	
}